/*
 * FreeEdit.cpp
 * Authors: 107 <107@mail.ru>
 * License: GPL >=2.0
 *
 *
 * The main goal of this file is to manipulate other forms
 * Only Application and Form-based logic may be placed in this file
 *
 */


#include "FreeEdit.h"
#include "macros.h"

using namespace Osp::App;
using namespace Osp::Base;
using namespace Osp::System;
using namespace Osp::Graphics;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;


// constructor
FreeEdit::FreeEdit() {
}

// destructor
FreeEdit::~FreeEdit() {
}

// public Create Instance
Application* FreeEdit::CreateInstance(void) {
	// Create the instance through the constructor.
	return new FreeEdit();
}

bool FreeEdit::OnAppInitializing(AppRegistry& appRegistry) {
	// Create a form
	pMainForm = new MainForm();
	pMainForm->Initialize();
	pMainForm->setParent( *this );

	// Add the form to the frame
	pFrame = GetAppFrame()->GetFrame();
	pFrame->AddControl( *pMainForm );

	// File Dialog
	pFileDialog = new badadev::FileDialog();
	pFileDialog->Initialize( S( "IDS_FILE_DIALOG" ), *this, "txt" );
	pFileDialog->SetShowState( false );
	pFrame->AddControl( *pFileDialog );

	// Editor Form
	pEditor = new EditorForm();
	pEditor->Initialize( *this );
	pEditor->SetShowState( false );
	pFrame->AddControl( *pEditor );

	// Set the current form
	pFrame->SetCurrentForm( *pMainForm );

	// Draw and Show the form
	pMainForm->Draw();
	pMainForm->Show();

	return true;
}

void FreeEdit::receiveContainerActionFromChild(const int actionId,InheritanceContainer &child) {
	switch(actionId) {
			case MainForm::ON_OPEN_FILE: {
				AppLog( "OpenFile" );
				pFrame->SetCurrentForm( *pFileDialog );
				pMainForm->SetShowState( false );
				pFileDialog->SetTitleText( S( "IDS_LOAD" ) );
				pFileDialog->IsLoad = true;
				pFileDialog->SetShowState( true );
				pFileDialog->GoTop();
				pFileDialog->Show();
				pFrame->RequestRedraw( true );
			}
			break;

			case MainForm::ON_SAVE_AS: {
				AppLog( "Save file" );
				//	Save As
				pFrame->SetCurrentForm( *pFileDialog );
				pMainForm->SetShowState( false );
				pFileDialog->SetShowState( true );
				pFileDialog->SetTitleText( S( "IDS_SAVE" ) );
				pFileDialog->IsLoad = false;
				pFileDialog->GoTop();
				pFileDialog->Show();
				pFrame->RequestRedraw( );
				}
			break;

			case badadev::FileDialog::ON_SELECTED: {
				AppLog( "selected" );
				pFrame->SetCurrentForm( *pMainForm );
				pFileDialog->SetShowState( false );
				pMainForm->SetShowState( true );
				pMainForm->Draw();

				if(pFileDialog->getSelectedFilePath()!="")	{
					if ( pFileDialog->IsLoad) {
						AppLog( "load from file %S", pFileDialog->getSelectedFilePath().GetPointer() );
						pMainForm->LoadFromFile( pFileDialog->getSelectedFilePath() );
						pFileDialog->GoTop();
						pMainForm->SetSavedStatus( true );
					} else {
						AppLog( "save to file %S", pFileDialog->getSelectedFilePath().GetPointer() );
						pMainForm->SaveToFile( pFileDialog->getSelectedFilePath() );
						pFileDialog->GoTop();
						pMainForm->SetSavedStatus( true );
					}
				}
			}
			break;

			case badadev::FileDialog::ON_CLOSE_ACTION: {
				AppLog( " canceled 0" );
				pFileDialog->GoTop();
				pFrame->SetCurrentForm( *pMainForm );
				pFileDialog->SetShowState( false );
				pMainForm->SetShowState( true );
				pFrame->RequestRedraw( true );
			}
			break;

			case MainForm::ON_EDITOR_OPEN: {
				AppLog( "Editor open" );
				pFrame->SetCurrentForm( *pEditor );
				pMainForm->SetShowState( false );
				pEditor->SetText( pMainForm->GetCurrentBuff() );
				pEditor->SetShowState( true );
				pEditor->Show();
				pFrame->RequestRedraw( true );
			}
			break;

			case EditorForm::ID_EDITOR_CLOSE: {
				AppLog( "Editor close" );
				pFrame->SetCurrentForm( *pMainForm );
				pEditor->SetShowState( false );
				pMainForm->SetShowState( true );
				pFrame->RequestRedraw( );
			}
			break;

			case EditorForm::ID_EDITOR_SAVE: {
				AppLog( "Editor save" );
				pFrame->SetCurrentForm( *pMainForm );
				pEditor->SetShowState( false );
				pMainForm->SetShowState( true );
				pFrame->RequestRedraw( );
				pMainForm->InsertText( pEditor->GetText() );
				pMainForm->SetSavedStatus( false );
			}
			break;

			default:
				AppLog ( "default action on FreeEdit recieve" );
			break;

		}
}

bool FreeEdit::OnAppTerminating(AppRegistry& appRegistry, bool forcedTermination) {
	// TODO:
	// Deallocate resources allocated by this application for termination.
	// The application's permanent data and context can be saved via appRegistry.
	return true;
}

void FreeEdit::OnForeground(void) {
	// TODO:
	// Start or resume drawing when the application is moved to the foreground.
}

void FreeEdit::OnBackground(void) {
	// TODO:
	// Stop drawing when the application is moved to the background.
}

void FreeEdit::OnLowMemory(void) {
	// TODO:
	// Free unused resources or close the application.
}

void FreeEdit::OnBatteryLevelChanged(BatteryLevel batteryLevel) {
	if ( batteryLevel >= BATTERY_CRITICAL ) {
		pMainForm->Save();
	}
}

void FreeEdit::OnScreenOn (void) {
	// TODO:
	// Get the released resources or resume the operations that were paused or stopped in OnScreenOff().
}

void FreeEdit::OnScreenOff (void) {
	// TODO:
	//  Unless there is a strong reason to do otherwise, release resources (such as 3D, media, and sensors) to allow the device to enter the sleep mode to save the battery.
	// Invoking a lengthy asynchronous method within this listener method can be risky, because it is not guaranteed to invoke a callback before the device enters the sleep mode.
	// Similarly, do not perform lengthy operations in this listener method. Any operation must be a quick one.
}
